<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理</title>
    <link rel="stylesheet" type="text/css" href="style/Back_Management.css">
    <link href="layui/css/layui.css" rel="stylesheet" media="all">
    <script src="layui/layui.js"></script>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<header>
    <img src="image/logo.png" id="logo">
    <p class="logo_word">读了么</p>
    <p class="moment_admin">当前管理员昵称：${sessionScope.admin.adminName}</p>
</header>
<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <ul class="layui-tab-title">
        <li class="layui-this">用户管理</li>
        <li onclick="userInfo()">用户信息管理</li>
        <li onclick="shopManage()">店铺管理</li>
        <li onclick="bookManage()">商品管理</li>
    </ul>
    <script>
        layui.use('element', function () {
            var $ = layui.jquery
                , element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块

            //触发事件
            var active = {};
            element.on('tab(test)', function (elem) {
                location.hash = 'test=' + $(this).attr('lay-id');
            });
        });
    </script>
    <div class="layui-tab-content" style="margin-left: 10%;margin-right: 10%">
        <div class="layui-tab-item layui-show">
            <table id="userAcc" lay-filter="test"></table>
            <script type="text/html" id="tbDel1">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
            <script>
                layui.use('table', function () {
                    var table = layui.table;
                    table.render({
                        elem: '#userAcc',
                        height: 405,
                        method:'post',
                        page: true,
                        url: '/data/admin',//获取数据的接口
                        where:{method:'user'},
                        cols: [[
                            {field: 'userId', title: '用户ID', sort: true, fixed: 'left'},
                            {field: 'userAccount', title: '用户账号', sort: true},
                            {field: 'userName', title: '用户昵称', edit: 'text'},
                            {field: 'avatar', title: '用户头像',edit: 'text'},
                            {field: 'createDate', title: '创建时间'},
                            {fixed: 'right', align: 'center', toolbar: '#tbDel1'}
                        ]],
                    });
                    //修改
                    table.on('edit(test)', function (obj) {
                        var data = obj.data;
                        var userId = data.userId;
                        var userAccount = data.userAccount;
                        var userName = data.userName;
                        var avatar = data.avatar;
                        var createDate = data.createDate;
                        $.ajax({
                            url: '/update/admin',//修改用户账号的接口
                            type: 'post',
                            dataType: 'json',
                            data: {
                                'method':'modify',
                                'target':'user',
                                'userId': userId,
                                'userAccount': userAccount,
                                'userName': userName,
                                'avatar': avatar,
                                'createDate': createDate
                            },
                            success: function (data) {
                                if (data.result == '1') {
                                    alert("修改成功！");
                                }else alert("修改失败！");
                            }
                        })
                    });
                    //删除
                    table.on('tool(test)', function (obj) {
                        var data = obj.data;
                        var layEvent = obj.event; //获得 lay-event 对应的值
                        var tr = obj.tr;
                        if (layEvent === 'del') {
                            layer.confirm('确认删除？', function (index) {
                                var userId = data.userId;
                                obj.del();
                                $.ajax({
                                    url: '/update/admin',//删除用户账号的接口
                                    type: 'post',
                                    dataType: 'json',
                                    data: {userId: userId,method:'delete',target:'user'},
                                    success: function (data) {
                                        if (data.result == 1) {
                                            alert("删除成功！");
                                            table.reload('userAcc',{url:'/data/admin',where:{method:'user'}})
                                        }
                                    }
                                });
                                layer.close(index)
                            })
                        }
                    });
                });
            </script>
        </div>
        <div class="layui-tab-item">
            <table id="userInfo" lay-filter="info"></table>
            <script type="text/html" id="initialize">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">初始化</a>
            </script>
            <script>
                function userInfo(){
                    layui.use('table', function () {
                        var table = layui.table;
                        table.render({
                            elem: '#userInfo',
                            height: 405,
                            method:'post',
                            page: true,
                            where:{method:'userInfo'},
                            url: '/data/admin',//获取数据的接口
                            cols: [[
                                {field: 'userId', title: '用户ID', sort: true, fixed: 'left'},
                                {field: 'name', title: '姓名', sort: true},
                                {field: 'birthday', title: '生日',edit: 'text'},
                                {field: 'sex', title: '性别',edit: 'text'},
                                {field: 'phoneNumber', title: '联系电话',edit: 'text'},
                                {field: 'emailAddress', title: '电子邮箱', sort: true, edit: 'text'},
                                {field: 'id', title: '身份证号', width: '100'},
                                {fixed: 'right', align: 'center', toolbar: '#initialize'}
                            ]],
                        });
                        //修改
                        table.on('edit(info)', function (obj) {
                            var data = obj.data;
                            var userId = data.userId;
                            var name = data.name;
                            var birthday = data.birthday;
                            var sex = data.sex;
                            var phoneNumber = data.phoneNumber;
                            var emailAddress = data.emailAddress;
                            var id = data.id;
                            $.ajax({
                                url: '/update/admin',//修改用户信息接口
                                type: 'post',
                                dataType: 'json',
                                data: {
                                    'method':'modify',
                                    'target':'userInfo',
                                    'userId': userId,
                                    'name': name,
                                    'birthday': birthday,
                                    'sex': sex,
                                    'phoneNumber': phoneNumber,
                                    'emailAddress': emailAddress,
                                    'id': id
                                },
                                success: function (data) {
                                    if (data.result == '1') {
                                        alert("修改成功！");
                                    }else alert("修改失败！")
                                }
                            })
                        });
                        table.on('tool(info)', function (obj) {
                            var data = obj.data;
                            var layEvent = obj.event; //获得 lay-event 对应的值
                            var tr = obj.tr;
                            if (layEvent === 'del') {
                                layer.confirm('确认初始化用户信息？', function (index) {
                                    var userId = data.userId;
                                    var name = '';
                                    var birthday = '';
                                    var sex = '';
                                    var phoneNumber = '';
                                    var emailAddress = '';
                                    var id = '';
                                    $.ajax({
                                        url: '/update/admin',//初始化用户信息的接口
                                        type: 'post',
                                        dataType: 'json',
                                        data: {
                                            'method':'delete',
                                            'target':'userInfo',
                                            'userId': userId,
                                            'name': name,
                                            'birthday': birthday,
                                            'sex': sex,
                                            'phoneNumber': phoneNumber,
                                            'emailAddress': emailAddress,
                                            'id': id
                                        },
                                        success: function (data) {
                                            if (data.result == 1) {
                                                alert("初始化成功！");
                                                table.reload('userInfo',{url:'/data/admin',where:{method:'userInfo'}})
                                            }else alert('初始化失败！')
                                        }
                                    });
                                    layer.close(index)
                                })
                            }
                        });
                    })
                }

            </script>
        </div>
        <div class="layui-tab-item">
            <table id="shop" lay-filter="dshelf"></table>
            <script type="text/html" id="down">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">下架</a>
            </script>
            <script>
                function shopManage(){
                    layui.use('table', function () {
                        var table = layui.table;
                        table.render({
                            elem: '#shop',
                            height: 405,
                            method:'post',
                            page: true,
                            where:{method:'shop'},
                            url: '/data/admin',//获取数据的接口
                            cols: [[
                                {field: 'shopId', title: '店铺ID', sort: true, fixed: 'left'},
                                {field: 'shopName', title: '店铺名称', sort: true, edit: 'text'},
                                {field: 'createTime', title: '创建时间', sort: true},
                                {field: 'ownerId', title: '所有人ID', width: '120'},
                                {field: 'totalSell', title: '总销量', width: '120', sort: true},
                                {field: 'count', title: '商品数量', width: '100', sort: true},
                                {field: 'shopAvatar', title: '店铺头像', width: '100', edit: 'text'},
                                {fixed: 'right', width: 125, align: 'center', toolbar: '#down'}
                            ]],
                        });
                        //修改
                        table.on('edit(dshelf)', function (obj) {
                            var data = obj.data;
                            var shopId = data.shopId;
                            var shopName = data.shopName;
                            var createTime = data.createTime;
                            var ownerId = data.ownerId;
                            var totalSell = data.totalSell;
                            var count = data.count;
                            var shopAvatar = data.shopAvatar;
                            $.ajax({
                                url: '/update/admin',//修改店铺信息接口
                                type: 'post',
                                dataType: 'json',
                                data: {
                                    'method':'modify',
                                    'target':'shop',
                                    'shopId': shopId,
                                    'shopName': shopName,
                                    'createTime': createTime,
                                    'ownerId': ownerId,
                                    'totalSell': totalSell,
                                    'count': count,
                                    'shopAvatar': shopAvatar
                                },
                                success: function (data) {
                                    if (data.result == 1) {
                                        alert("修改成功！");
                                    }else alert("修改失败！")
                                }
                            })
                        });
                        //关闭店铺
                        table.on('tool(dshelf)', function (obj) {
                            var data = obj.data;
                            var layEvent = obj.event; //获得 lay-event 对应的值
                            var tr = obj.tr;
                            if (layEvent === 'del') {
                                layer.confirm('确认关闭这个店铺？', function (index) {
                                    var shopId = data.shopId;
                                    obj.del();
                                    $.ajax({
                                        url: '/update/admin',//初始化用户信息的接口
                                        type: 'post',
                                        dataType: 'json',
                                        data: {'shopId': shopId,'method':'delete','target':'shop'},
                                        success: function (data) {
                                            if (data.result == 1) {
                                                alert("关闭店铺成功！");
                                            }else alert('关闭店铺失败！');
                                        }
                                    });
                                    layer.close(index)
                                })
                            }
                        });
                    })
                }

            </script>
        </div>
        <div class="layui-tab-item">
            <table id="book" lay-filter="test1"></table>
            <script type="text/html" id="bdown">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">下架</a>
            </script>

            <script>
                function bookManage(){
                    layui.use('table', function () {
                        var table = layui.table;
                        table.render({
                            elem: '#book',
                            height: 405,
                            page: true,
                            method:'post',
                            where:{method:'book'},
                            url: '/data/admin',//获取数据的接口
                            cols: [[
                                {field: 'bookId', title: '书籍ID', fixed: 'left', sort: true},
                                {field: 'bookName', title: '书名', sort: true, edit: "text"},
                                {field: 'shopName', title: '书店名称', sort: true},
                                {field: 'synopsis', title: '书籍简介', edit: "text"},
                                {field: 'author', title: '作者', edit: "text"},
                                {field: 'press', title: '出版社', edit: "text"},
                                {field: 'publishDate', title: '出版时间', edit: "text"},
                                {field: 'cover', title: '书籍图片', edit: "text"},
                                {field: 'category', title: '书籍类别', edit: "text", sort: true},
                                {field: 'price', title: '价格', width: '100', sort: true, edit: 'text'},
                                {field: 'remain', title: '库存', width: '100', sort: true, edit: 'text'},
                                {field: 'sell', title: '销量', width: '80', sort: true},
                                {fixed: 'right', width: 125, align: 'center', toolbar: '#bdown'}
                            ]],
                        });
                        //修改
                        table.on('edit(test1)', function (obj) {
                            var data=obj.data;
                            var bookId = data.bookId;
                            var bookName = data.bookName;
                            var synopsis = data.synopsis;
                            var author = data.author;
                            var press = data.press;
                            var publishDate = data.publishDate;
                            var cover = data.cover;
                            var shopName = data.shopName;
                            var shopId = data.shopId;
                            var sell = data.sell;
                            var remain = data.remain;
                            var price = data.price;
                            var category = data.category;
                            $.ajax({
                                url: '/update/admin',//修改书籍接口
                                type: 'post',
                                dataType: 'json',
                                data: {
                                    'method':'modify',
                                    'target':'book',
                                    'bookId': bookId,
                                    'bookName': bookName,
                                    'synopsis': synopsis,
                                    'author': author,
                                    'press': press,
                                    'publishDate': publishDate,
                                    'cover': cover,
                                    'shopName': shopName,
                                    'shopId': shopId,
                                    'sell': sell,
                                    'remain': remain,
                                    'price': price,
                                    'category': category,
                                },
                                success: function (data) {
                                    if (data.result == 1) {
                                        alert("修改成功！");
                                    }else alert("修改失败");
                                }
                            })
                        });
                        //下架书籍
                        table.on('tool(test1)', function (obj) {
                            var data = obj.data;
                            var layEvent = obj.event; //获得 lay-event 对应的值
                            var tr = obj.tr;
                            if (layEvent === 'del') {
                                layer.confirm('确认下架这本书？', function (index) {
                                    var shopId = data.shopId;
                                    var bookId = data.bookId;
                                    obj.del();
                                    $.ajax({
                                        url: '/update/admin',//下架书籍的接口
                                        type: 'post',
                                        dataType: 'json',
                                        data: {'method':'delete','target':'book','bookId': bookId, 'shopId': shopId,},
                                        success: function (data) {
                                            if (data.result == 1) {
                                                alert("下架成功！");
                                            }else alert("下架失败！")
                                            table.reload('book',{url:"/data/admin",where:{method:'book'}})
                                        }
                                    });
                                    layer.close(index)
                                })
                            }
                        });
                    })
                }

            </script>
        </div>
    </div>
</div>
</body>
</html>
